package com.krtech.wecard.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.system.SystemUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class DfsUtil {
    private Integer timeout;

    public DfsUtil() {
    }

    public DfsUtil(Integer timeout) {
        this.timeout = timeout;
    }

    @Data
    public static class DfsFile {
        private String domain;
        private String md5;
        private Long mtime;
        private Long retcode;
        private Long size;
        private String path;
        private String retmsg;
        private String scene;
        private String scenes;
        private String src;
        private String url;
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    public DfsFile upload(File file) {
        return upload(file, "group1", null);
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    public DfsFile uploadWithName(File file, String fileName) {
        return upload(file, "group1", fileName);
    }

    /**
     * 上传文件
     *
     * @param file
     * @param groupName
     * @return
     */
    public DfsFile upload(File file, String groupName) {
        return upload(file, groupName, null);
    }

    public DfsFile upload(File file, String groupName, String fileName) {
        String dfsHome = SystemUtil.get("DFS_HOME", "http://10.10.101.246:43745/");
        String url = dfsHome + groupName + "/upload?output=json";
        if (fileName != null) {
            url = url +  "&filename=" + fileName;
        }
        log.info("upload Url:{}", url);
        HttpRequest post = HttpUtil.createPost(url);
        post.form("file", file);
        String jsonStr = post.execute().body();
        log.info("upload rest:{}", jsonStr);
        return JSONUtil.toBean(jsonStr, DfsFile.class);
    }

    /**
     * 获取文件后缀
     *
     * @param url
     * @return
     */
    private String findSuffix(String url) {
        return ReUtil.getGroup1(RegexConstant.URL_SUFFIX_REG, url);
    }

    /**
     * 下载文件
     *
     * @param url
     * @return
     */
    public File download(String url) {
        return this.download(url,true);
    }
    public File download(String url,Boolean needSuffix) {
        log.info("download Url:{}", url);
        if (url.startsWith("/group1")) {
            String dfsHome = SystemUtil.get("DFS_HOME", "http://10.10.101.240:22100");
            url = dfsHome + url;
            log.info("download Fixed Url:{}", url);
        }

        File file;
        if(needSuffix) {
            String suffix = findSuffix(url);
            file = FileUtil.createTempFile("dfs", suffix, new File("/tmp/"), true);
        }else {
            file = FileUtil.createTempFile("dfs", ".tmp", new File("/tmp/"), true);
        }
        if (timeout == null) {
            HttpUtil.downloadFile(url, file);
        } else {
            HttpUtil.downloadFile(url, file, timeout);
        }
        log.info("file download to {}", file.getAbsolutePath());
        return file;
    }

    /**
     * 下载文件
     *
     * @param url
     * @return
     */
    public File download(String url, File file) {
        log.info("download Url:{}", url);
        if (url.startsWith("/group1")) {
            String dfsHome = SystemUtil.get("DFS_HOME", "http://10.10.101.240:22100");
            url = dfsHome + url;
            log.info("download Fixed Url:{}", url);
        }

        String suffix = findSuffix(url);
        if (timeout == null) {
            HttpUtil.downloadFile(url, file);
        } else {
            HttpUtil.downloadFile(url, file, timeout);
        }
        log.info("file download to {}", file.getAbsolutePath());
        return file;
    }
}
